//
//  RJWarningAQICheckView.m
//  AirQuality
//
//  Created by Po on 15/4/14.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJWarningPM25CheckView.h"
#import "RJWarningPM25CheckDay.h"
#import "RJTools.h"
@interface RJWarningPM25CheckView () <RJWarningFatherDeleaget>

@property (retain, nonatomic) NSMutableArray * daysArray;

- (void)initInterface;

@end

@implementation RJWarningPM25CheckView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self initInterface];
        [self setFatherDelegate:self];
    }
    return self;
}

- (void)initInterface
{
    _daysArray = [[NSMutableArray alloc] init];
    NSUInteger dayCount = 1;
    for (NSUInteger i = 0; i < 3 ; i ++) {
        RJWarningPM25CheckDay * day = [[RJWarningPM25CheckDay alloc] initWithFrame:CGRectMake(0, 160 *  i, ScreenWidth, 140)];
        [day.timeLabel setText:[RJTools getTimeWithFormat:@"MM月dd日" time:GetFutureSinceNow(dayCount)]];
        [self addSubview:day];
        [_daysArray addObject:day];
        dayCount ++;
    }
}

#pragma mark - RJWarningFatherDelegate
- (void)WarningFatherSetData:(NSArray *)data
{
    BOOL isDone = [RJUserData defaultRJUserData].isDone;
    BOOL isAudit = [RJUserData defaultRJUserData].isAudit;
    if (isDone || isAudit) {
        NSArray * dataArray = data[0];
        NSArray * detailArray = [self getDaysDetailWithData:data[1]];
        for (NSUInteger i = 0; i < 3; i ++) {
            RJWarningPM25CheckDay * day = _daysArray[i];
            [day setDetailData:detailArray[i]];
            
            CGFloat number = [dataArray[i] floatValue];
            [day.concentrationLabel setText:GetStringWithFloat(number)];
            
            //计算区间
            
            NSUInteger mathNum = [RJTools getPM25MathNum:number];
            NSNumber * pm25Max = @(number + mathNum);
            NSNumber * pm25Min = @(number - mathNum);
            pm25Min = [pm25Min integerValue] < 0 ? @0 : pm25Min;
            [day.concentrationRangeLabel setText:[NSString stringWithFormat:@"%@~%@",pm25Min, pm25Max]];
            
            NSNumber * aqiMax = @([RJTools getAQINumber:[pm25Max floatValue]]);
            NSNumber * aqiMin = @([RJTools getAQINumber:[pm25Min floatValue]]);
            [day.AQIRangeLabel setText:[NSString stringWithFormat:@"%@~%@",aqiMin, aqiMax]];

        }
    }
}

- (void)WarningFatherSetUserData:(NSDictionary *)data
{
}

- (void)WarningFatherWillRemoveSelf
{
}

- (NSDictionary *)WarningFatherGetData
{
    NSMutableArray * pm25DR = [NSMutableArray array];
    NSMutableArray * pm25IR = [NSMutableArray array];
    for (NSUInteger i = 0; i < 3; i ++) {
        RJWarningPM25CheckDay * day = _daysArray[i];
        [pm25DR addObject:day.concentrationRangeLabel.text];
        [pm25IR addObject:day.AQIRangeLabel.text];
    }
    return @{@"pm2_5_dr":pm25DR,@"pm2_5_ir":pm25IR};
}

- (NSArray *)getDaysDetailWithData:(NSArray *)data
{
    NSMutableArray * daysDetailArray = [[NSMutableArray alloc] init];
    for (NSUInteger i = 0; i < 3; i ++) {
        NSMutableArray * array = [[NSMutableArray alloc] init];
        for (NSDictionary * dic in data) {
            NSString * checkPM25 = [NSString stringWithFormat:@"浓度 - %@",dic[@"PM2_5"][i]];
            NSString * checkPM25DR = [NSString stringWithFormat:@"浓度区间 - %@",dic[@"PM2_5_DR"][i]];
            NSString * checkPM25IR = [NSString stringWithFormat:@"AQI范围 - %@",dic[@"PM2_5_IR"][i]];
            [array addObject:@{dic[@"USERNAME"]:@[checkPM25,checkPM25DR,checkPM25IR]}];
        }
        [daysDetailArray addObject:array];
    }
    return daysDetailArray;
}
@end
